import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../generated/l10n.dart';
import '../../res/colors.dart';
import '../../res/text_style.dart';
import 'over_scroll_behavior.dart';

/// @description :公共通用弹窗
class CommonDialog extends StatelessWidget {
  ///标题
  String title = '';

  ///内容
  String content = '';

  ///左侧文字
  String negaText = '';

  ///右侧文字
  String posiText = '';

  ///左侧事件
  VoidCallback? negaTap;

  ///右侧事件
  VoidCallback? posiTap;

  ///左侧是否隐藏
  bool negaVisible = true;

  ///右侧是否隐藏
  bool posiVisible = true;

  Widget? centerWidget;

  CommonDialog({
    super.key,
    this.title = '',
    this.content = '',
    this.negaText = '',
    this.posiText = '',
    this.negaVisible = true,
    this.posiVisible = true,
    this.negaTap,
    this.posiTap,
    this.centerWidget,
  });

  @override
  Widget build(BuildContext context) {
    return Material(

        ///透明样式
        type: MaterialType.transparency,

        ///dialog居中
        child: Center(

            ///取消ListView滑动阴影
            child: ScrollConfiguration(
                behavior: OverScrollBehavior(),

                ///ListView 的shrinkWrap属性可适应高度（有多少占多少）
                child: ListView(
                  shrinkWrap: true,
                  children: [
                    ///背景及内容、边距、圆角等，必须包裹在ListView中
                    Container(
                      width: double.infinity,
                      // margin: EdgeInsets.symmetric(horizontal: 50.w),
                      margin: EdgeInsets.only(right: 50.w, left: 50.w),
                      child: Container(
                        decoration: const ShapeDecoration(
                          color: Colors.white,
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.all(
                              Radius.circular(20.0),
                            ),
                          ),
                        ),
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.start,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: <Widget>[
                            ///标题、内容

                            title.isNotEmpty
                                ? Text(
                                    title,
                                    textAlign: TextAlign.center,
                                    style: TextStyles.textStyle(
                                        ColorStyle.black, 16.sp,
                                        fontWeight: FontWeight.w600),
                                  ).marginOnly(top: 20.h)
                                : Container(),
                            Padding(
                              padding: EdgeInsets.symmetric(
                                  horizontal: 17.w, vertical: 17.h),
                              child: centerWidget ??
                                  Text(
                                    content,
                                    textAlign: TextAlign.center,
                                    style: TextStyles.textStyle(
                                        ColorStyle.color303030, 14.sp),
                                  ),
                            ),

                            ///确定、取消按钮
                            Flex(
                              mainAxisAlignment: MainAxisAlignment.spaceAround,
                              direction: Axis.horizontal,
                              children: [
                                ///对半分
                                _textButton(
                                        negaTap,
                                        negaText.isNotEmpty
                                            ? negaText
                                            : S.current.cancel,
                                        const Color(0xFF333333),
                                        const Color(0xffeeeeee))
                                    .marginOnly(right: 20.w),

                                _textButton(
                                    posiTap,
                                    posiText.isNotEmpty
                                        ? posiText
                                        : S.current.confirm,
                                    Colors.white,
                                    const Color(0xff1877ff)),
                              ],
                            ).marginOnly(bottom: 20.h)
                          ],
                        ),
                      ),
                    ),
                  ],
                ))));
  }

  Widget _textButton(
      VoidCallback? tap, String text, Color textColor, Color btuColor) {
    return GestureDetector(
      onTap: () {
        Get.back();
        if (tap != null) {
          tap();
        }
      },
      child: Container(
        alignment: Alignment.center,
        padding:
            EdgeInsets.only(left: 16.w, right: 16.w, top: 6.h, bottom: 6.h),
        decoration: BoxDecoration(
          color: btuColor,
          borderRadius: BorderRadius.circular(4.r),
        ),
        child: Text(
          text,
          style: TextStyle(
              color: textColor, fontWeight: FontWeight.w500, fontSize: 14.sp),
        ),
      ),
    );
  }
}
